package com.uchengwang.ui.utils;

import android.text.TextUtils;

import com.uchengwang.functions.http.entity.MapRequest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by U城网 on 2017/7/24.
 * 提交网络时 decode key object
 */

public class MapKeyObjectUtils {

    private static MapKeyObjectUtils utils;
    private MapKeyObjectUtils (){}

    public static MapKeyObjectUtils getUtils() {
        if (utils == null){
            utils = new MapKeyObjectUtils();
        }
        return utils;
    }

    /**
     * 针对某些特定的字段不需要decode的方法
     * @param map key object 集合
     * @param list 不需要decode的key集合
     * */
    public String decodeMap(MapRequest map, List<String> list){
        if (map == null){
            return null;
        }

        StringBuilder tempParams = new StringBuilder();
        try {
            if (map != null) {
                HashMap<String, Object> data = map.getData();
                if (data != null) {
                    int size = data.size();
                    Iterator iter = data.keySet().iterator();
                    if (size == 1){//就一个字段
                        String key = (String) iter.next();
                        String val = (String) data.get(key);

                        tempParams.append("/");
                        tempParams.append(String.format("%s", URLEncoder.encode(URLEncoder.encode(val, "utf-8"), "utf-8")));

                    }else {//多个字段
                        boolean isFirst = true;
                        while (iter.hasNext()) {
                            String key = (String) iter.next();
                            String val = (String) data.get(key);

                            if (isFirst){
                                tempParams.append("?");
                                isFirst = false;
                            }else {
                                tempParams.append("&");
                            }
                            if (TextUtils.isEmpty(val)){
                                continue;
                            }

                            if (list != null && list.size() >0){
                                //判断改字段是否需要decode
                                boolean isDecode = false;
                                for (String s :list){
                                    if (s.equals(key)){
                                        //说明该字段不需要decode
                                        isDecode = true;
                                        tempParams.append(String.format("%s=%s", key, val));
                                        break;
                                    }
                                }
                                if (!isDecode){
                                    tempParams.append(String.format("%s=%s", key, URLEncoder.encode(URLEncoder.encode(val, "utf-8"), "utf-8")));
                                }
                            }else {
                                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(URLEncoder.encode(val, "utf-8"), "utf-8")));
                            }

                        }
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return tempParams.toString();
    }

}
